/**
 * Created by vlean on 2018/10/11.
 */
var XMPP = require('node-xmpp');
// var cl = new XMPP.Client({
//     jid: 'user1@127.0.0.1',
//     password: '1234',
//     host: '127.0.0.1',
//     port: '5222'
// });
// cl.on("error", function (err) {
//     console.log(err);
// });


const ht = '127.0.0.1';
// const ht = 'dolinpa.com'

var client = new XMPP.Client({jid: `user1@${ht}`, password: '1234', host: ht, port: '5222'})

client.connection.socket.on('error', function (error) {
    console.error(error, 123)
    process.exit(1)
})

client.on("authenticate", function(opts, cb) {
    console.log("AUTH" + opts.jid + " -> " +opts.password);
    cb(null, opts);
});

client.on('online', function (data) {
    console.log('Connected as ' + data.jid.local + '@' + data.jid.domain + '/' + data.jid.resource)
    argv.slice(5).forEach(function (to) {
        var stanza = new Client.Stanza('message', {to: to, type: 'chat'})
            .c('body').t(argv[4])
        client.send(stanza)
    })

    // nodejs has nothing left to do and will exit
    client.end()
})

client.on('error', function (err) {
    console.error(err, 456)
    process.exit(1)
})
